package com.changpay.zywy.ui.activity.mine

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.BankCardAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.bean.BankCardBean
import com.changpay.zywy.utils.GsonUtils
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_bank_card.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap

class BankCardActivity : BaseActivity(), IView {

    private var type = 0 // 0,点击跳转解绑 1 点击提现
        private val onClickListener = View.OnClickListener { view ->
            when (view.id) {
                R.id.ll_add_card -> {
                    val intent = Intent(this, PayPasswordActivity::class.java)
                    intent.putExtra("activity_title", "添加银行卡第一步")
                    intent.putExtra("message", "请输入6位数支付密码，以验证身份")
                    intent.putExtra("title", "添加您的银行卡")
                    intent.putExtra("imp_type", 1)
                    startActivity(intent)
                }
            }
        }

    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
    }

    override fun showMsg(msg: String) {
    }

    override fun showError(errorMsg: String) {
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_bank_card
    }

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "我的银行卡"
        iv_back.setOnClickListener { finish() }
        type = intent.getIntExtra("type", 0)
        initRecyclerView()
        ll_add_card.setOnClickListener(onClickListener)

    }

    override fun start() {
        loadData()
    }

    private fun loadData() {
        // 加载底部数据
        val params = HashMap<String, String>()
        params["key"] = token
        RetrofitHelper.service.get_member_bank(params).sss(this) {
            datas.clear()
            datas.addAll(it.result)
            mAdapter.notifyDataSetChanged()
        }

    }


    /**
     * datas
     */
    private val datas = mutableListOf<BankCardBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: BankCardAdapter by lazy {
        BankCardAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas.size != 0) {
            val data = datas[position]
            if (type == 0) {
                val intent = Intent(this, UnBindBankCardActivity::class.java)
                intent.putExtra("bank_name", data.bank_name)
                intent.putExtra("bank_number", data.bank_card)
                intent.putExtra("bank_id", data.card_id.toString())
                startActivity(intent)
            } else if(type ==1) {
                val intent = Intent()
                intent.putExtra("info", GsonUtils.serializedToJson(data))
                setResult(200, intent)
                finish()
            }

        }
    }


    private fun initRecyclerView() {
        rv_bank_card_list.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(rv_bank_card_list)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            this.onItemClickListener = ItemClickListener
        }
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }
}